package com.coda.controller;

import com.coda.adminVo.system.user.SystemUserItemVo;
import com.coda.entity.SystemUser;
import com.coda.mapper.SystemUserMapper;
import com.coda.result.PageResult;
import com.coda.result.Result;
import com.coda.service.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Target;

@RestController
@RequestMapping("/admin/system/user")
@Api(tags = "后台用户信息管理")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;
    /**
     * 条件分页查询后台用户列表
     */
    @GetMapping("/page")
    @ApiOperation("条件分页查询后台用户列表")
    public Result<PageResult> getPageList(@RequestParam Integer current,
                                          @RequestParam Integer size,
                                          SystemUser systemUser){
        PageResult pageResult = systemUserService.getPageList(current,size,systemUser);
        return Result.ok(pageResult);
    }

    /**
     * 根据id查询后台用户信息
     */
    @GetMapping("/getById")
    @ApiOperation("根据id查询后台用户信息")
    public Result<SystemUserItemVo> getById(Long id){
        SystemUserItemVo systemUserItemVo = systemUserService.getById(id);
        return Result.ok(systemUserItemVo);
    }

    /**
     * 保存或更新后台用户信息
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存或更新后台用户信息")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser){
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }
    /**
     * 判断用户名是否可用
     */
    @GetMapping("/isUserNameAvailable")
    @ApiOperation("判断用户名是否可用")
    public Result isUsernameExists(@RequestParam String username){
        systemUserService.isUsernameExists(username);
        return Result.ok(true);
    }
    /**
     * 修改后台用户状态
     */
    @PostMapping("/updateStatusByUserId")
    @ApiOperation("修改后台用户状态")
    public Result updateStatusByUserId(@RequestParam Long id,@RequestParam Integer status){
        systemUserService.updateStatusByUserId(id,status);
        return Result.ok();
    }
    /**
     * 根据id删除后台用户信息
     */
    @DeleteMapping("/deleteById")
    @ApiOperation("根据id删除后台用户信息")
    public Result deleteById(Long id){
        systemUserService.deleteById(id);
        return Result.ok();
    }

}
